package com.express.controller;

import com.express.bean.BootStrapTableExpress;
import com.express.bean.Express;
import com.express.bean.Message;
import com.express.bean.ResultData;
import com.express.mvc.ResponseBody;
import com.express.service.ExpressService;
import com.express.util.DateFormatUtil;
import com.express.util.JSONUtil;
import com.express.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.SplittableRandom;

/**
 * @Author: laixiangdong
 * @Date 2020/12/06
 */
public class ExpressController {
    @ResponseBody("/express/console.do")
    public String console(HttpServletRequest req, HttpServletResponse resp) {

        List<Map<String, Integer>> data = ExpressService.console();
        Message message = null;
        if (data == null)
            message = new Message(-1, "获取数据失败", null);
        else
            message = new Message(0, "获取数据成功", data);
        String s = JSONUtil.toJSON(message);
        return s;
    }

    @ResponseBody("/express/list.do")
    public String list(HttpServletRequest req, HttpServletResponse resp) {
        int offset = 0;
        int pageNumber = 10;
        try {
            // 起始索引值
            offset = Integer.parseInt(req.getParameter("offset"));
            // 查询的数量
            pageNumber = Integer.parseInt(req.getParameter("pageNumber"));
        } catch (NumberFormatException e) {
            System.out.println("获取offset，pageNumber值异常，默认值0，10");
        }

        // 查询
        List<Express> list = ExpressService.findAll(true, offset, pageNumber);
        List<BootStrapTableExpress> list2 = new ArrayList<>();
        for (Express e : list) {
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime() == null ? "-" : DateFormatUtil.format(e.getOutTime());
            String status = e.getStatus() == 0 ? "待取件" : "已取件";
            // int id, String number, String userName, String userPhone, String company, String code, String inTime, String outTime, String status, String sysPhone
            BootStrapTableExpress e2 = new BootStrapTableExpress(e.getId(), e.getNumber(), e.getUserName(), e.getUserPhone(), e.getCompany(), e.getCode(), inTime, outTime, status, e.getSysPhone());
            list2.add(e2);
        }
        List<Map<String, Integer>> console = ExpressService.console();
        Integer total = console.get(0).get("data1_size");

        // 封装为bootstrap-table可识别格式
        ResultData<BootStrapTableExpress> data = new ResultData<>();
        data.setRows(list2);
        data.setTotal(total);
        String s = JSONUtil.toJSON(data);
        return s;
    }

    @ResponseBody("/express/insert.do")
    public String insert(HttpServletRequest req, HttpServletResponse resp) {
        String number = req.getParameter("number");
        String company = req.getParameter("company");
        String userName = req.getParameter("userName");
        String userPhone = req.getParameter("userPhone");
        // String number, String userName, String userPhone, String company, String sysPhone
        System.out.println(UserUtil.getUserName(req.getSession()));
        Express e = new Express(number, userName, userPhone, company, UserUtil.getUserPhone(req.getSession()));
        Boolean flag = ExpressService.insert(e);
        Message message = new Message();
        if (flag) {
            message.setStatus(0);
            message.setResult("录入成功");
        } else {
            message.setStatus(-1);
            message.setResult("录入失败");
        }
        String s = JSONUtil.toJSON(message);
        return s;
    }

    @ResponseBody("/express/find.do")
    public String find(HttpServletRequest req, HttpServletResponse resp) {
        String number = req.getParameter("number");
        Express express = ExpressService.findByNumber(number);
        Message message = new Message();
        if (express != null) {
            message.setStatus(0);
            message.setResult("查询成功");
            message.setData(express);
        } else {
            message.setStatus(-1);
            message.setResult("没有查询到数据");
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/express/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp) {
        int id = Integer.parseInt(req.getParameter("id"));
        String number = req.getParameter("number");
        String company = req.getParameter("company");
        String userName = req.getParameter("userName");
        String userPhone = req.getParameter("userPhone");
        int status = Integer.parseInt(req.getParameter("status"));
        Express express = new Express();
        express.setUserName(userName);
        express.setUserPhone(userPhone);
        express.setStatus(status);
        express.setNumber(number);
        express.setCompany(company);
        Boolean flag = ExpressService.update(id, express);
        Message message = new Message();
        if (flag) {
            message.setStatus(0);
            message.setResult("修改成功");
        } else {
            message.setStatus(-1);
            message.setResult("修改失败");
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/express/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        Boolean flag = ExpressService.delete(id);
        Message message = new Message();
        if (flag){
            message.setStatus(0);
            message.setResult("删除成功");
        }else {
            message.setStatus(-1);
            message.setResult("删除失败");
        }
        return JSONUtil.toJSON(message);
    }
}
